﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MDIDemo.Windows.Report
{
    public partial class frmBaoCaoCongNoPS : DevExpress.XtraEditors.XtraForm
    {
        Dictionary<string, object> _valueList = new Dictionary<string, object>();

        public Dictionary<string, object> ValueList
        {
            get { return _valueList; }
            set { _valueList = value; }
        }
        DataSet _ds = null;
        public DataSet DataSource { get { return _ds; } }
        public string Type
        {
            get
            {
                return rType.EditValue.ToString();
            }

        }

        public frmBaoCaoCongNoPS()
        {
            InitializeComponent();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            Close();
        }
        public DataSet BuildDataSourceNXHD()
        {
            string query = "";
            if (rType.EditValue.ToString() == "BCCNPSPTHU")
            {
                query = @"select isnull(SOTIEN,0) as SOTIEN, isnull(PST,0) as PST, isnull(PSG,0) as PSG,MAKH, TENGD,  NGAYCT,  SOCT, DIACHIKH 
from (SELECT    
(select  SUM(dk.SOTIEN) as SOTIEN FROM  CONGNO as dk where (dk.NGAYCT < '{0:yyyy-MM-dd}') AND (dk.SOCTTT = 'BH') and cn.MAKH =dk.MAKH and cn.DIACHIKH = dk.DIACHIKH ) 
AS SOTIEN, 
(SELECT     SUM(cnpst.SOTIEN) AS SOTIEN
FROM         CONGNO as cnpst
WHERE     (cnpst.NGAYCT = '{0:yyyy-MM-dd}')  AND (cnpst.SOCTTT = 'BH') and cn.MAKH =cnpst.MAKH and cn.DIACHIKH = cnpst.DIACHIKH AND  ((cnpst.PHAITHU = 'True' AND cnpst.D_C = 'C') OR (cnpst.PHAITHU = 'False' AND  cnpst.D_C = 'N'))                       
 ) AS PST, 
(SELECT     SUM(cnpsg.SOTIEN) AS SOTIEN
FROM         CONGNO  as cnpsg
WHERE     (cnpsg.NGAYCT = '{0:yyyy-MM-dd}')  AND (cnpsg.SOCTTT = 'BH') AND   (cnpsg.PHAITHU = 'False') and cn.MAKH =cnpsg.MAKH and cn.DIACHIKH = cnpsg.DIACHIKH AND  (cnpsg.D_C = 'C' OR cnpsg.D_C ='')                       
) AS PSG, 
cn.MAKH, CUSTINFO.TENGD, MAX(cn.NGAYCT) AS NGAYCT, MAX(cn.SOCT) AS SOCT, cn.DIACHIKH
FROM         CONGNO as cn INNER JOIN
                      CUSTINFO ON cn.MAKH = CUSTINFO.CUSTID
WHERE       (cn.NGAYCT <= '{0:yyyy-MM-dd}') AND (cn.SOCTTT = 'BH') 
GROUP BY cn.MAKH, CUSTINFO.TENGD, cn.DIACHIKH) as tmp
WHERE abs(PST)>0 or abs(PSG)>0 or abs(SOTIEN)>0
";
                query = string.Format(query, txtDateF.EditValue, txtDateF.EditValue);
            }
            else
            {
                query = @"exec procCONGNO_getAllCongNoPSTRA @DateF='{0:yyyy-MM-dd}',@DateT='{1:yyyy-MM-dd}'";
                query = string.Format(query, txtDateF.EditValue, txtDateF.EditValue);
            }
            BUS.CommonControl ctr = new BUS.CommonControl();
            _ds = ctr.executeSelectQueries(query);
            return _ds;
        }
        private void btnPrint_Click(object sender, EventArgs e)
        {
            _ds = BuildDataSourceNXHD();

            if (_ds.Tables.Count == 0)
            {
                XtraMessageBox.Show("Dữ liệu không tồn tại!");
                return;
            }
            bool isdata = false;
            foreach (DataTable dt in _ds.Tables)
            {
                if (dt.Rows.Count > 0)
                {
                    isdata = true;
                    break;
                }
            }
            if (isdata == false)
            {
                XtraMessageBox.Show("Dữ liệu không tồn tại!");
                return;
            }
            _valueList.Add("DateF", txtDateF.EditValue);
            _valueList.Add("DateT", txtDateF.EditValue);
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }


        private void frmBangKeGTGT_Load(object sender, EventArgs e)
        {
            txtDateF.EditValue = frmMain.__ngayMoSo;
           
        }
    }
}